/*
 * @Description: 文件内容描述
 * @Author: 李志杰
 * @Date: 2020-03-23 16:42:49
 * @LastEditTime: 2020-03-30 18:32:30
 * @LastEditors: huacong
 */
import _ from 'lodash';

export const buildTrees = (list, pid = '') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (x.selectable === 'true') x.selectable = true;
    if (x.selectable === 'false') x.selectable = false;
    if (`${x.parentOrgid}` === `${pid}`) {
      x.children = buildTrees(list, x.orgdepid);
      tree.push({
        ...x,
        key: x.orgdepid,
        value: x.orgdepid,
        title: x.orgname,
      });
    }
  });
  return tree;
};

export const getTreeDatas = list => {
  let treeData = [];
  if (list && list.length > 0) {
    treeData = buildTrees(
      list,
      _.filter(list, x => x.parentOrgid === '00000000').length > 0 ? '00000000' : '00000001',
    );
  }
  return treeData;
};

export const getDefaultSelected = (list, tree = []) => {
  _.each(list, x => {
    if (tree.length <= 0) {
      if (x.selectable) {
        tree.push(x);
        return false;
      }
      getDefaultSelected(x.children, tree);
    }
  });
  return tree;
};

export const getTreeOrgnameList = (tree, list = []) => {
  _.each(tree, x => {
    list.push(x.orgname);
    if (x.children && x.children.length > 0) getTreeOrgnameList(x.children, list);
  });
  return list;
};
